package RecursiveSearcher;

/**
 * @ClassName LC129
 * @Description 求根节点到叶子结点的数字之和
 * @Author ZJX
 * @Date 2024/9/12 20:23
 * @Version 1.0
 */

//  https://leetcode.cn/problems/sum-root-to-leaf-numbers/description/
public class LC129 {
    public int sumNumbers(TreeNode root) {
        return dfs(root,0);
    }

    private int dfs(TreeNode node, int sum) {
        if (node == null){
            return 0;
        }
        sum = sum * 10 + node.val;
        if (node.left == null && node.right == null){
            return sum;
        }
        return dfs(node.left,sum) + dfs(node.right,sum);
    }
}
